
package com.ttomato.game.framework.math;

public class Rectangle {
    public Vector2 mLowerLeft;

    public float mWidth, mHeight;

    public Rectangle(float x, float y, float width, float height) {
        mLowerLeft.mX = x;
        mLowerLeft.mY = y;
        mWidth = width;
        mHeight = height;
    }

    public boolean overlapRectangle(Rectangle r1, Rectangle r2) {
        if (r1.mLowerLeft.mX < r2.mLowerLeft.mX + r2.mWidth
                && r1.mLowerLeft.mX + r1.mWidth > r2.mLowerLeft.mX
                && r1.mLowerLeft.mY < r2.mLowerLeft.mY + r2.mHeight
                && r1.mLowerLeft.mY + r1.mHeight > r2.mLowerLeft.mY) {
            return true;
        }
        return false;
    }

    public boolean overlapCircleRectangle(Circle circle, Rectangle rectangle) {
        float closestX = circle.mCenter.mX;
        float closestY = circle.mCenter.mY;

        if (closestX < rectangle.mLowerLeft.mX) {
            closestX = rectangle.mLowerLeft.mX;
        }

        if (closestX > rectangle.mLowerLeft.mX + rectangle.mWidth) {
            closestX = rectangle.mLowerLeft.mX + rectangle.mWidth;
        }

        if (closestY < rectangle.mLowerLeft.mY) {
            closestY = rectangle.mLowerLeft.mY;
        }

        if (closestY > rectangle.mLowerLeft.mY + rectangle.mHeight) {
            closestY = rectangle.mLowerLeft.mY + rectangle.mHeight;
        }

        return circle.mCenter.distSquared(closestX, closestY) < circle.mRadius * circle.mRadius;
    }
}
